//
//  SocialUtils.cpp
//  WXDemo
//
//  Created by Joueu on 14-11-6.
//
//

#include "SocialUtils.h"

USING_NS_CC;

// ============================公开调用的统一接口============================
void SocialUtils::registerWeixin(char* appID){
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    // Android
    SocialUtils::registerWeixinForAndroid(appID);
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    // IOS
   SocialUtils::registerWeixinForIOS(appID);
#endif
}

void SocialUtils::shareToWeiXin(){
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    // Android
    SocialUtils::shareToWeixinForAndroid();
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    // IOS
    SocialUtils::shareToWeixinForIOS();
#endif
}

//void SocialUtils::shareToFacebook(){
//#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//    // Android
//    SocialUtils::shareToFacebookForAndroid();
//#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
//    // IOS
//    SocialUtils::shareToFacebookforIOS();
//#endif
//}

void SocialUtils::destroy(){
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    // Android
    SocialUtils::destroyForAndroid();
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    // IOS
//    SocialUtils::destroyForIOS();
#endif
}


// ============================【微信】Android通过Jni调用java层接口============================
// 说明：其中的Utils::getAndroidPackageInfo()返回的是java完整的类包名。具体涉及到Jni的知识，同学们可以参考：http://blog.csdn.net/lognic10/article/details/22269537 学习一下。

#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
void SocialUtils::registerWeixinForAndroid(char *appID){
    JniMethodInfo jmi;
    bool ishave = JniHelper::getStaticMethodInfo(jmi,"tk/helkyle/plantnanny","registerWeixin","(Ljava/lang/String;)V");
    
    if(ishave)
    {
        // 将C++的char*转换成java的jstring
        jstring str_id = jmi.env->NewStringUTF(appID);
        jmi.env->CallStaticVoidMethod(jmi.classID,jmi.methodID,str_id);
    }
}

void SocialUtils::shareToWeixinForAndroid(){
    JniMethodInfo jmi;
    bool ishave = JniHelper::getStaticMethodInfo(jmi,"tk/helkyle/plantnanny","shareToWeixin","()V");
    
    if(ishave)
    {
        jmi.env->CallStaticVoidMethod(jmi.classID,jmi.methodID);
    }
}

void SocialUtils::setShareType(int type)
{
    if (type == 0) {
        _scene = WXSceneTimeline;
    }else{
        _scene = WXSceneSession;
    }
}
// ============================【Facebook】Android通过Jni调用java层接口============================

//void SocialUtils::shareToFacebookForAndroid(){
//    JniMethodInfo jmi;
//    bool ishave = JniHelper::getStaticMethodInfo(jmi,Utils::getAndroidPackageInfo(),"shareToFacebook","()V");
//    
//    if(ishave)
//    {
//        jmi.env->CallStaticVoidMethod(jmi.classID,jmi.methodID);
//    }
//}
//
//void SocialUtils::destroyForAndroid(){
//    JniMethodInfo jmi;
//    bool ishave = JniHelper::getStaticMethodInfo(jmi,Utils::getAndroidPackageInfo(),"onDestroy","()V");
//    
//    if(ishave)
//    {
//        jmi.env->CallStaticVoidMethod(jmi.classID,jmi.methodID);
//    }
//}
#endif